
<template>
    <el-dialog :title="formData.id ? '编辑' : '新增'" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
        <div>
            <el-form :model="formData" ref="form" label-width="80px">
                
                <el-form-item label="ID" prop="id">
                    <el-input v-model="formData.id"></el-input>
                </el-form-item>
                
                <el-form-item label="标题" prop="title">
                    <el-input v-model="formData.title"></el-input>
                </el-form-item>
                
                <el-form-item label="作者" prop="author">
                    <el-input v-model="formData.author"></el-input>
                </el-form-item>
                
                <el-form-item label="日期" prop="display_time">
                    <el-input v-model="formData.display_time"></el-input>
                </el-form-item>
                
                <el-form-item label="浏览量" prop="pageviews">
                    <el-input v-model="formData.pageviews"></el-input>
                </el-form-item>
                
            </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import { basicRequest } from '@/api/basics'
export default {
    data() {
        return {
            dialogVisible: false,
            formData: {
                id: '',
                title: '',
                author: '',
                display_time: '',
                pageviews: '',
                
            },
        }
    },
    methods: {
        init(row) {
            this.dialogVisible = true
            if(!row.id) {
                this.$nextTick(() => {
                    this.$refs.form.resetFields()
                })
                return
            }
            this.formData.id = row.id
            this.getFormData()
        },
        getFormData() {
            basicRequest({ id: this.formData.id }, {}, '/vue-admin-template/table/get', 'GET').then(res => {
                this.formData = res.data;
            })
        },
        handleClose() {
            this.$refs.form.resetFields();

            this.$nextTick(() => {
                this.dialogVisible = false;
            })
        },
        submit() {
            const url = this.formData.id ? '/vue-admin-template/table/edit' : '/vue-admin-template/table/add'
            const method = this.formData.id ? 'PUT' : 'POST'
            basicRequest(this.formData, {}, url, method).then(res => {
                this.$message.success('操作成功');
                this.$emit('refresh');
                this.handleClose()
            })
        },
    }
}
</script>
    